package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.QueryRoleEnum;
import cc.rengu.igas.momp.common.enums.QueryTypeEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.common.enums.TableNameEnum;
import cc.rengu.igas.momp.common.model.bean.JsonTmApsMcRuleInfo;
import cc.rengu.igas.momp.common.model.bean.JsonTmpApsMcInfo;
import cc.rengu.igas.momp.common.model.bean.JsonTmpMcOrgInfo;
import cc.rengu.igas.momp.common.util.CommonMessageCheck;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.igas.momp.facade.bean.AgentProfitSharingMcInfoBean;
import cc.rengu.igas.momp.facade.bean.AgentProfitSharingRuleInfoBean;
import cc.rengu.igas.momp.facade.bean.McOrgInfoBean;
import cc.rengu.igas.momp.facade.request.QueryAgentProfitSharingMcDetailInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryAgentProfitSharingMcDetailInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代理商分润类活动详细信息查询功能实现
 *
 * @author Jinan Liu
 * @date 2020/3/24 15:19
 */
public class QueryAgentProfitSharingMcDetailInfoService extends RadpService {

    private String globalInstId;
    private String globalQueryType;
    private String globalQueryRole;
    private String globalQueryId;
    private String globalQueryOrgId;
    private String globalMcNo;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryAgentProfitSharingMcDetailInfoRequest queryAgentProfitSharingMcDetailInfoRequest = new QueryAgentProfitSharingMcDetailInfoRequest();
            ConvertUtil.convertOutput(queryAgentProfitSharingMcDetailInfoRequest);

            QueryAgentProfitSharingMcDetailInfoResponse queryAgentProfitSharingMcDetailInfoResponse = new QueryAgentProfitSharingMcDetailInfoResponse();
            queryAgentProfitSharingMcDetailInfoResponse.setHeader(queryAgentProfitSharingMcDetailInfoRequest.getHeader());
            BizResponse<QueryAgentProfitSharingMcDetailInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryAgentProfitSharingMcDetailInfoResponse);

            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalQueryType = queryAgentProfitSharingMcDetailInfoRequest.getQueryType();
            globalQueryId = queryAgentProfitSharingMcDetailInfoRequest.getQueryId();
            globalQueryOrgId = queryAgentProfitSharingMcDetailInfoRequest.getQueryOrgId();
            globalQueryRole = queryAgentProfitSharingMcDetailInfoRequest.getQueryRole();
            globalInstId = queryAgentProfitSharingMcDetailInfoRequest.getHeader().getInstId();
            globalMcNo = queryAgentProfitSharingMcDetailInfoRequest.getMcNo();
            rglog.info("机构<{}>在<{}>由<{}>开始查询代理商分润类营销活动详细信息流程", globalInstId, timeStamp, globalQueryId);

            /* 报文检查 */
            CommonMessageCheck.checkBizMessageHeader(globalQueryType, globalQueryId, globalQueryRole, rglog);

            /* 检查操作员所属机构号 */
            if (StringUtil.isNullorEmpty(globalQueryOrgId)) {
                rglog.error("法人机构号<{}>的操作员<{}>的所属机构为空!", globalInstId, globalQueryId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }

            /* 检查营销活动编号 */
            if (StringUtil.isNullorEmpty(globalMcNo)) {
                rglog.error("法人机构号<{}>的操作员<{}>的营销活动编号为空!", globalInstId, globalQueryId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }

            // 服务调用
            BizResponse<QueryAgentProfitSharingMcDetailInfoResponse> bizResponseNew = queryAgentProfitSharingMcDetailInfo(xmlTreeUtil, queryAgentProfitSharingMcDetailInfoRequest, queryAgentProfitSharingMcDetailInfoResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 代理商分润类活动详细信息查询
     *
     * @param xmlTreeUtil                                 内部XML树
     * @param queryAgentProfitSharingMcDetailInfoRequest  代理商分润类活动详情查询接口请求报文
     * @param queryAgentProfitSharingMcDetailInfoResponse 代理商分润类活动详情查询接口应答报文
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryAgentProfitSharingMcDetailInfoResponse>代理商分润类活动详情查询接口应答报文
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 14:50
     */

    private BizResponse<QueryAgentProfitSharingMcDetailInfoResponse> queryAgentProfitSharingMcDetailInfo(XmlTreeUtil xmlTreeUtil, QueryAgentProfitSharingMcDetailInfoRequest queryAgentProfitSharingMcDetailInfoRequest, QueryAgentProfitSharingMcDetailInfoResponse queryAgentProfitSharingMcDetailInfoResponse)
            throws Exception {

        BizResponse<QueryAgentProfitSharingMcDetailInfoResponse> bizResponse = new BizResponse<>();


        /* 判断查询类型 */
        MoMcListInfoInObj moMcListInfoInObj = new MoMcListInfoInObj();

        /* 机构编码 */
        moMcListInfoInObj.setInstId(globalInstId);

        /* 营销活动编号 */
        moMcListInfoInObj.setMcNo(globalMcNo);


        /* 先查询所有数据，在判断查询用户是否有权限获取到数据 */
        /* 根据角色权限查询数据 */
        switch (QueryTypeEnum.getByValue(globalQueryType)) {
            case BANK_QUERY_TYPE:
                /* 银行人员,需要拼接营销活动机构信息表查询 */

                /* 机构编号 */
                if (!StringUtil.isNullorEmpty(globalQueryOrgId)) {
                    moMcListInfoInObj.setOrgId(globalQueryOrgId);
                }

                /* 银行人员查询营销活动详细信息 */
                queryMoneyOffMcDetailInfoByBank(queryAgentProfitSharingMcDetailInfoRequest, queryAgentProfitSharingMcDetailInfoResponse, moMcListInfoInObj);

                break;

            case AGENT_QYERY_TYPE:
                /* 代理商,需要拼接营销活动商户信息表查询 */

                /* 根据商户编号查询商圈编号 */
                String mchntNo = globalQueryId;
                String mchntArea = null;
                TBMchntBaseInfo tbMchntBaseInfo = null;
                if (!StringUtil.isNullorEmpty(mchntNo)) {
                    /* 查询代理商基本信息表 */
                    tbMchntBaseInfo = selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
                    if (null == tbMchntBaseInfo) {
                        rglog.error("查询法人机构<{}>代理商<{}>基本信息失败!", globalInstId, mchntNo);
                        throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                    } else {
                        /* 代理商编号 */
                        if (!StringUtil.isNullorEmpty(mchntNo)) {
                            moMcListInfoInObj.setMchntNo(mchntNo);
                        }

                        /* 商圈编号 */
                        mchntArea = tbMchntBaseInfo.getMchntArea();
                        if (!StringUtil.isNullorEmpty(mchntArea)) {
                            moMcListInfoInObj.setMchntArea(mchntArea);
                        }
                    }
                }

                /* 商户查询营销活动详细信息 */

                queryMoneyOffMcDetailInfoByMchnt(queryAgentProfitSharingMcDetailInfoRequest, queryAgentProfitSharingMcDetailInfoResponse, moMcListInfoInObj);

                break;

            default:
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryAgentProfitSharingMcDetailInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryAgentProfitSharingMcDetailInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryAgentProfitSharingMcDetailInfoResponse);

        return bizResponse;

    }

    /**
     * 银行人员查询营销活动详细信息
     *
     * @param queryAgentProfitSharingMcDetailInfoRequest  代理商分润类活动详情查询接口请求报文
     * @param queryAgentProfitSharingMcDetailInfoResponse 代理商分润类活动详情查询接口应答报文
     * @param moMcListInfoInObj                           营销活动列表查询使用实体类入参
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 18:58
     */
    private void queryMoneyOffMcDetailInfoByBank(QueryAgentProfitSharingMcDetailInfoRequest queryAgentProfitSharingMcDetailInfoRequest, QueryAgentProfitSharingMcDetailInfoResponse queryAgentProfitSharingMcDetailInfoResponse, MoMcListInfoInObj moMcListInfoInObj) throws Exception {

        if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(globalQueryRole) ||
                QueryRoleEnum.BANK_PUBLISH_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
            TMMcAgentProfitSharingInfo mcAgentProfitSharingInfo;

            if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {

                /* 银行录入岗流程 */
                mcAgentProfitSharingInfo = selectApsInfoDetailInfoByBankBasicRole(moMcListInfoInObj);
                if (null == mcAgentProfitSharingInfo) {
                    rglog.error("机构<{}>的代理商分润类营销活动<{}>的基本信息查询失败!用户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
                    throw new BizException(RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespDesc());
                }
            } else {

                /* 银行发布岗流程 */
                mcAgentProfitSharingInfo = selectApsInfoDetailInfoByBankPublishRole(moMcListInfoInObj);
                if (null == mcAgentProfitSharingInfo) {
                    rglog.error("机构<{}>的代理商分润类营销活动<{}>的基本信息查询失败!用户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
                    throw new BizException(RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespDesc());
                }
            }

            /* 银行操作员和发布员都是从正式表查询数据,除了营销活动基本表外其他表查询语句相同 */

            /* 查询代理商营销活动信息 */
            AgentProfitSharingMcInfoBean agentProfitSharingMcInfoBean = convertMcInfo(mcAgentProfitSharingInfo);

            /* 查询营销活动机构信息 */
//            TMMcOrgInfo mcOrgInfo = getAllMcOrgListInfo(queryAgentProfitSharingMcDetailInfoRequest);
//            McOrgInfoBean mcOrgInfoBean = convertMcOrgInfo(mcOrgInfo);
            List<TMMcOrgInfo> mcOrgInfoList = getAllMcOrgListInfo();
            if (null == mcOrgInfoList || mcOrgInfoList.isEmpty()) {
                rglog.error("机构<{}>的代理商分润类营销活动<{}>的机构信息查询失败!", globalInstId, globalMcNo);
                throw new BizException(RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespDesc());
            }
            List<McOrgInfoBean> mcOrgInfoBeanList = mcOrgInfoList.stream().map(this::convertMcOrgInfo).collect(Collectors.toList());
            //按照机构层级排序,插入营销活动信息的时候OrgHierarchy字段,取排序后的最后一个
            mcOrgInfoBeanList.sort(Comparator.comparingInt(x -> Integer.parseInt(x.getOrgHierarchy())));
            List<McOrgInfoBean> newList = new ArrayList<>();
            newList.add(mcOrgInfoBeanList.get(mcOrgInfoBeanList.size() - 1));
            /* 查询营销活动规则信息 */
            List<TMMcAgentProfitSharingRuleInfo> mcAgentProfitSharingRuleInfoList = new ArrayList<>();
            getAllMcRuleListInfo(queryAgentProfitSharingMcDetailInfoRequest, mcAgentProfitSharingRuleInfoList);
            List<AgentProfitSharingRuleInfoBean> agentProfitSharingRuleInfoBeanList = new ArrayList<>();
            if (null != mcAgentProfitSharingRuleInfoList && !mcAgentProfitSharingRuleInfoList.isEmpty()) {
                agentProfitSharingRuleInfoBeanList = mcAgentProfitSharingRuleInfoList.stream().map(this::convertMcRuleInfo).collect(Collectors.toList());
            }

            /* 拼接交易信息 */
            queryAgentProfitSharingMcDetailInfoResponse.setMcInfoObj(agentProfitSharingMcInfoBean);
            queryAgentProfitSharingMcDetailInfoResponse.setOrgInfoList(newList);
            queryAgentProfitSharingMcDetailInfoResponse.setRuleInfoList(agentProfitSharingRuleInfoBeanList);

        } else if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {

            /* 银行审批岗流程 */
            TMPlatBizTmp tmPlatBizTmp = selectApsMcDetailInfoByBankAuditRole(moMcListInfoInObj);
            if (null == tmPlatBizTmp) {
                rglog.error("机构<{}>的代理商分润类营销活动<{}>的基本信息查询失败!用户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
                throw new BizException(RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespDesc());
            }

            /* 取临时表中所有数据 */
            List<TMPlatBizTmp> tmPlatBizTmpList = getAllMcTmpInfo(moMcListInfoInObj);
            if (null == tmPlatBizTmpList || tmPlatBizTmpList.isEmpty()) {
                rglog.error("机构<{}>的代理商分润类营销活动<{}>的基本信息查询失败!用户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
                throw new BizException(RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespDesc());
            }

            /* 拆分数据 */

            /* 转换营销活动基本信息 */
            AgentProfitSharingMcInfoBean agentProfitSharingMcInfoBean = convertTmpMcInfo(tmPlatBizTmp);

            /* 转换营销活动机构信息 */
            List<TMPlatBizTmp> tmPlatBizTmpOrgList = tmPlatBizTmpList.stream().filter(record -> TableNameEnum.T_M_MC_ORG_INFO.getTableNameCode().equals(record.getOprTableEname())).collect(Collectors.toList());
            List<McOrgInfoBean> mcOrgInfoBeanList = tmPlatBizTmpOrgList.stream().map(this::convertTmpMcOrgInfo).collect(Collectors.toList());
            //按照机构层级排序,插入营销活动信息的时候OrgHierarchy字段,取排序后的最后一个
            mcOrgInfoBeanList.sort(Comparator.comparingInt(x -> Integer.parseInt(x.getOrgHierarchy())));
            List<McOrgInfoBean> newList = new ArrayList<>();
            newList.add(mcOrgInfoBeanList.get(mcOrgInfoBeanList.size() - 1));
//            McOrgInfoBean mcOrgInfoBeanList = convertTmpMcOrgInfo(tmPlatBizTmp);

            /* 查询营销活动规则信息 */
            List<TMPlatBizTmp> tmPlatBizTmpRuleList = tmPlatBizTmpList.stream().filter(record -> TableNameEnum.T_M_MC_APS_RULE_INFO.getTableNameCode().equals(record.getOprTableEname())).collect(Collectors.toList());
            List<AgentProfitSharingRuleInfoBean> agentProfitSharingRuleInfoBeanList = tmPlatBizTmpRuleList.stream().map(this::convertTmpMcRuleInfo).collect(Collectors.toList());

            /* 拼接交易信息 */
            queryAgentProfitSharingMcDetailInfoResponse.setMcInfoObj(agentProfitSharingMcInfoBean);
            queryAgentProfitSharingMcDetailInfoResponse.setOrgInfoList(newList);
            queryAgentProfitSharingMcDetailInfoResponse.setRuleInfoList(agentProfitSharingRuleInfoBeanList);

        } else {
            /* 其他查询角色不支持 */
            rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }

    }

    /**
     * 商户查询营销活动详细信息
     *
     * @param queryAgentProfitSharingMcDetailInfoRequest  代理商分润类活动详情查询接口请求报文
     * @param queryAgentProfitSharingMcDetailInfoResponse 代理商分润类活动详情查询接口应答报文
     * @param moMcListInfoInObj                           营销活动列表查询使用实体类入参
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:23
     */
    private void queryMoneyOffMcDetailInfoByMchnt(QueryAgentProfitSharingMcDetailInfoRequest queryAgentProfitSharingMcDetailInfoRequest, QueryAgentProfitSharingMcDetailInfoResponse queryAgentProfitSharingMcDetailInfoResponse, MoMcListInfoInObj moMcListInfoInObj) throws Exception {

        /* 查询营销活动基本信息 */
        TMMcMoneyOffInfo tmMcMoneyOffInfo = selectMoneyOffMcDetailInfoByMchnt(moMcListInfoInObj);
        if (StringUtil.isNullorEmpty(moMcListInfoInObj.getMchntArea())) {
            tmMcMoneyOffInfo = selectMoneyOffMcDetailInfoByMchnt(moMcListInfoInObj);
        } else {
            tmMcMoneyOffInfo = selectMoneyOffMcDetailInfoByMchntWithArea(moMcListInfoInObj);
        }
        if (null == tmMcMoneyOffInfo) {
            rglog.error("机构<{}>的代理商分润类营销活动<{}>的基本信息查询失败!商户无权限查看数据或不存在该营销活动", globalInstId, globalMcNo);
            throw new BizException(RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespDesc());
        }

        /* 查询代理商营销活动信息 */
        TMMcAgentProfitSharingInfo mcAgentProfitSharingInfo = getMcInfo(queryAgentProfitSharingMcDetailInfoRequest);

        /* 查询营销活动机构信息 */
//        TMMcOrgInfo mcOrgInfo = getAllMcOrgListInfo(queryAgentProfitSharingMcDetailInfoRequest);
        List<TMMcOrgInfo> mcOrgInfoList = getAllMcOrgListInfo();
//        if (null == mcOrgInfoList || mcOrgInfoList.isEmpty()) {
//            rglog.error("机构<{}>的代理商分润类营销活动<{}>的机构信息查询失败!", globalInstId, globalMcNo);
//            throw new BizException(RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespDesc());
//        }
        List<McOrgInfoBean> mcOrgInfoBeanList = new ArrayList<>();
        if (null != mcOrgInfoList && !mcOrgInfoList.isEmpty()) {
            mcOrgInfoBeanList = mcOrgInfoList.stream().map(this::convertMcOrgInfo).collect(Collectors.toList());
            //按照机构层级排序,插入营销活动信息的时候OrgHierarchy字段,取排序后的最后一个
            mcOrgInfoBeanList.sort(Comparator.comparingInt(x -> Integer.parseInt(x.getOrgHierarchy())));
        }

        /* 查询营销活动规则信息 */
        List<TMMcAgentProfitSharingRuleInfo> mcAgentProfitSharingRuleInfoList = new ArrayList<>();
        getAllMcRuleListInfo(queryAgentProfitSharingMcDetailInfoRequest, mcAgentProfitSharingRuleInfoList);
//        if (null == mcAgentProfitSharingRuleInfoList || mcAgentProfitSharingRuleInfoList.isEmpty()) {
//            rglog.error("机构<{}>的代理商分润类营销活动<{}>的分润类规则查询失败!", globalInstId, globalMcNo);
//            throw new BizException(RespCodeEnum.GET_MC_RULE_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_RULE_INFO_ERROR.getRespDesc());
//        }

        /* 拼接交易信息 */
        AgentProfitSharingMcInfoBean agentProfitSharingMcInfoBean = convertMcInfo(mcAgentProfitSharingInfo);
//        McOrgInfoBean mcOrgInfoBean = convertMcOrgInfo(mcOrgInfo);
        List<AgentProfitSharingRuleInfoBean> agentProfitSharingRuleInfoBeanList = new ArrayList<>();
        if (null != mcAgentProfitSharingRuleInfoList && !mcAgentProfitSharingRuleInfoList.isEmpty()) {
            agentProfitSharingRuleInfoBeanList = mcAgentProfitSharingRuleInfoList.stream().map(this::convertMcRuleInfo).collect(Collectors.toList());
        }
        queryAgentProfitSharingMcDetailInfoResponse.setMcInfoObj(agentProfitSharingMcInfoBean);
        List<McOrgInfoBean> nenList = new ArrayList<>();
        nenList.add(mcOrgInfoBeanList.get(mcOrgInfoBeanList.size() - 1));
        queryAgentProfitSharingMcDetailInfoResponse.setOrgInfoList(nenList);
        queryAgentProfitSharingMcDetailInfoResponse.setRuleInfoList(agentProfitSharingRuleInfoBeanList);
    }

    /**
     * 银行审批员查询营销活动基本信息表数据
     *
     * @param moMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMPlatBizTmp 业务数据临时表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:11
     */
    private TMPlatBizTmp selectApsMcDetailInfoByBankAuditRole(MoMcListInfoInObj moMcListInfoInObj) throws Exception {
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.selectApsMcDetailInfoByBankAuditRole(moMcListInfoInObj);
    }

    /**
     * 商户查询营销活动基本信息表数据
     *
     * @param moMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMMcMoneyOffInfo 代理商分润类营销活动信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:10
     */
    private TMMcMoneyOffInfo selectMoneyOffMcDetailInfoByMchnt(MoMcListInfoInObj moMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectMoneyOffMcDetailInfoByMchnt(moMcListInfoInObj);
    }

    /**
     * 商户查询营销活动基本信息表数据
     *
     * @param moMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMMcMoneyOffInfo 代理商分润类营销活动信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:10
     */
    private TMMcMoneyOffInfo selectMoneyOffMcDetailInfoByMchntWithArea(MoMcListInfoInObj moMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectMoneyOffMcDetailInfoByMchntWithArea(moMcListInfoInObj);
    }

    /**
     * @param queryAgentProfitSharingMcDetailInfoRequest
     * @param mcAgentProfitSharingRuleInfoList
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/25 22:37
     * @description 获取营销活动规则信息列表
     * @return: void
     */
    private void getAllMcRuleListInfo(QueryAgentProfitSharingMcDetailInfoRequest queryAgentProfitSharingMcDetailInfoRequest, List<TMMcAgentProfitSharingRuleInfo> mcAgentProfitSharingRuleInfoList) {

        String instId = queryAgentProfitSharingMcDetailInfoRequest.getHeader().getInstId();
        String mcNo = queryAgentProfitSharingMcDetailInfoRequest.getMcNo();

        try {
            McRuleInfoMapper mcRuleInfoMapper = new McRuleInfoMapperImpl();
            List<TMMcAgentProfitSharingRuleInfo> objectList = mcRuleInfoMapper.selectAgentProfitSharingRuleInfoList(instId, mcNo);
            if (null != objectList && !objectList.isEmpty()) {
                mcAgentProfitSharingRuleInfoList.addAll(objectList);
            } else {
                rglog.error("机构<{}>的代理商营销活动<{}>的代理商规则不存在!", instId, mcNo);
            }

        } catch (Exception e) {
            rglog.error("机构<{}>的代理商营销活动<{}>的代理商规则查询失败!", instId, mcNo);
            throw new BizException(RespCodeEnum.GET_MC_RULE_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_RULE_INFO_ERROR.getRespDesc());
        }
    }

    /**
     * 银行审批岗获取指定营销活动所有待审批数据
     *
     * @param moMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMPlatBizTmp> 业务数据临时表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:55
     */
    private List<TMPlatBizTmp> getAllMcTmpInfo(MoMcListInfoInObj moMcListInfoInObj) throws Exception {
        List<Object> tmMcMchntInfoList = new ArrayList<>();
        PageInfo pageInfo = new PageInfo();
        int i = 0;
        pageInfo.setPageIndex(i);
        pageInfo.setPageSize(9999);
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        PageResult tmMcMchntInfoPageInfo = platBizTmpMapper.selectSingleMcAllDetailInfoByBankAuditRole(moMcListInfoInObj, pageInfo);
        if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
            tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
        }
        while (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult() && tmMcMchntInfoPageInfo.getResult().size() >= 1) {
            i++;
            pageInfo.setPageIndex(i);
            tmMcMchntInfoPageInfo = platBizTmpMapper.selectSingleMcAllDetailInfoByBankAuditRole(moMcListInfoInObj, pageInfo);
            if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
                tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
            }
        }
        List<TMPlatBizTmp> tmpMcMchntInfoList = tmMcMchntInfoList.stream().map(item -> (TMPlatBizTmp) (item)).collect(Collectors.toList());
        return tmpMcMchntInfoList;
    }

    /**
     * @param mcAgentProfitSharingInfo
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/25 22:59
     * @description 营销活动信息数据库实体类和接口实体类数据转换
     * @return: cc.rengu.igas.momp.facade.bean.AgentProfitSharingMcInfo
     */
    private AgentProfitSharingMcInfoBean convertMcInfo(TMMcAgentProfitSharingInfo mcAgentProfitSharingInfo) {
        AgentProfitSharingMcInfoBean agentProfitSharingMcInfoBean = new AgentProfitSharingMcInfoBean();
        try {
            /* 营销活动编号 */
            agentProfitSharingMcInfoBean.setMcNo(mcAgentProfitSharingInfo.getMcNo());
            /* 营销活动名称 */
            agentProfitSharingMcInfoBean.setMcName(mcAgentProfitSharingInfo.getMcName());
            /* 营销活动类型 */
            agentProfitSharingMcInfoBean.setMcType(mcAgentProfitSharingInfo.getMcType());
            /* 营销活动状态 */
            agentProfitSharingMcInfoBean.setMcStatus(mcAgentProfitSharingInfo.getMcStatus());
            /* 活动起始日期 */
            agentProfitSharingMcInfoBean.setMcStartDate(mcAgentProfitSharingInfo.getMcStartDate());
            /* 活动截止日期 */
            agentProfitSharingMcInfoBean.setMcEndDate(mcAgentProfitSharingInfo.getMcEndDate());
            /* 活动结束日期 */
            agentProfitSharingMcInfoBean.setMcActualEndDate(mcAgentProfitSharingInfo.getMcActualEndDate());
            /* 代理商编号 */
            agentProfitSharingMcInfoBean.setAgentMchntNo(mcAgentProfitSharingInfo.getAgentMchntNo());
            /* 优惠打款模式 */
            agentProfitSharingMcInfoBean.setDiscountTransferMode(mcAgentProfitSharingInfo.getDiscountTransferMode());
            /* 活动预算 */
            agentProfitSharingMcInfoBean.setBudget(mcAgentProfitSharingInfo.getBudget());
            /* 达标条件 */
            agentProfitSharingMcInfoBean.setCriteriaFlag(mcAgentProfitSharingInfo.getCriteriaFlag());
            /* 有效交易达标金额 */
            agentProfitSharingMcInfoBean.setCriteriaLimit(mcAgentProfitSharingInfo.getCriteriaLimit());
            /* 统计周期 */
            agentProfitSharingMcInfoBean.setStatPeriod(mcAgentProfitSharingInfo.getStatPeriod());
            /* 新商户达标交易金额 */
            agentProfitSharingMcInfoBean.setCriteriaAmount(mcAgentProfitSharingInfo.getCriteriaAmount());
            /* 新商户达标交易笔数 */
            agentProfitSharingMcInfoBean.setCriteriaQuantity(mcAgentProfitSharingInfo.getCriteriaQuantity());
            /* 返现周期 */
            agentProfitSharingMcInfoBean.setCashbackPeriod(mcAgentProfitSharingInfo.getCashbackPeriod());
            /* 审批状态 */
            agentProfitSharingMcInfoBean.setAuditStatus(mcAgentProfitSharingInfo.getAuditStatus());
            /* 创建人 */
            agentProfitSharingMcInfoBean.setOprId(mcAgentProfitSharingInfo.getOprId());
            /* 创建时间 */
            agentProfitSharingMcInfoBean.setCreateTime(mcAgentProfitSharingInfo.getCreateTime());
            /* 最后修改人 */
            agentProfitSharingMcInfoBean.setLastOprId(mcAgentProfitSharingInfo.getLastOprId());
            /* 修改时间 */
            agentProfitSharingMcInfoBean.setUpdateTime(mcAgentProfitSharingInfo.getUpdateTime());
            /* 审批拒绝原因 */
            agentProfitSharingMcInfoBean.setAuditRefuseReason(mcAgentProfitSharingInfo.getAuditRefuseReason());
            /* 发布拒绝原因 */
            agentProfitSharingMcInfoBean.setPublishRefuseReason(mcAgentProfitSharingInfo.getPublishRefuseReason());
            /* 备用字段1 */
            agentProfitSharingMcInfoBean.setRemark1(mcAgentProfitSharingInfo.getRemark1());
            /* 备用字段2 */
            agentProfitSharingMcInfoBean.setRemark2(mcAgentProfitSharingInfo.getRemark2());
            /* 备用字段3 */
            agentProfitSharingMcInfoBean.setRemark2(mcAgentProfitSharingInfo.getRemark3());
            /* 备用字段4 */
            agentProfitSharingMcInfoBean.setRemark2(mcAgentProfitSharingInfo.getRemark4());
            /* 备用字段5 */
            agentProfitSharingMcInfoBean.setRemark2(mcAgentProfitSharingInfo.getRemark5());
            /* 强制终止原因*/
            agentProfitSharingMcInfoBean.setNfrcTmnlRsn(mcAgentProfitSharingInfo.getNfrcTmnlRsn());

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return agentProfitSharingMcInfoBean;
    }

    /**
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/25 22:59
     * @description 营销活动信息数据库实体类和接口实体类数据转换
     * @return: cc.rengu.igas.momp.facade.bean.AgentProfitSharingMcInfo
     */
    private AgentProfitSharingMcInfoBean convertTmpMcInfo(TMPlatBizTmp tmPlatBizTmp) {
        AgentProfitSharingMcInfoBean agentProfitSharingMcInfoBean = new AgentProfitSharingMcInfoBean();
        try {

            JsonTmpApsMcInfo jsonTmpApsMcInfo = JsonOperation.parApsMcInfoTmpBizData1(tmPlatBizTmp, rglog);
            /* 营销活动编号 */
            agentProfitSharingMcInfoBean.setMcNo(jsonTmpApsMcInfo.getMcNo());
            /* 营销活动名称 */
            agentProfitSharingMcInfoBean.setMcName(jsonTmpApsMcInfo.getMcName());
            /* 营销活动类型 */
            agentProfitSharingMcInfoBean.setMcType(jsonTmpApsMcInfo.getMcType());
            /* 营销活动状态 */
            agentProfitSharingMcInfoBean.setMcStatus(jsonTmpApsMcInfo.getMcStatus());
            /* 活动起始日期 */
            agentProfitSharingMcInfoBean.setMcStartDate(jsonTmpApsMcInfo.getMcStartDate());
            /* 活动截止日期 */
            agentProfitSharingMcInfoBean.setMcEndDate(jsonTmpApsMcInfo.getMcEndDate());
            /* 活动结束日期 */
            agentProfitSharingMcInfoBean.setMcActualEndDate(jsonTmpApsMcInfo.getMcActualEndDate());
            /* 代理商编号 */
            agentProfitSharingMcInfoBean.setAgentMchntNo(jsonTmpApsMcInfo.getAgentMchntNo());
            /* 优惠打款模式 */
            agentProfitSharingMcInfoBean.setDiscountTransferMode(jsonTmpApsMcInfo.getDiscountTransferMode());
            /* 活动预算 */
            agentProfitSharingMcInfoBean.setBudget(jsonTmpApsMcInfo.getBudget());
            /* 达标条件 */
            agentProfitSharingMcInfoBean.setCriteriaFlag(jsonTmpApsMcInfo.getCriteriaFlag());
            /* 有效交易达标金额 */
            agentProfitSharingMcInfoBean.setCriteriaLimit(jsonTmpApsMcInfo.getCriteriaLimit());
            /* 统计周期 */
            agentProfitSharingMcInfoBean.setStatPeriod(jsonTmpApsMcInfo.getStatPeriod());
            /* 新商户达标交易金额 */
            agentProfitSharingMcInfoBean.setCriteriaAmount(jsonTmpApsMcInfo.getCriteriaAmount());
            /* 新商户达标交易笔数 */
            agentProfitSharingMcInfoBean.setCriteriaQuantity(jsonTmpApsMcInfo.getCriteriaQuantity());
            /* 返现周期 */
            agentProfitSharingMcInfoBean.setCashbackPeriod(jsonTmpApsMcInfo.getCashbackPeriod());
            /* 审批状态 */
            agentProfitSharingMcInfoBean.setAuditStatus(tmPlatBizTmp.getAuditStatus());
            /* 创建人 */
            agentProfitSharingMcInfoBean.setOprId(tmPlatBizTmp.getOprId());
            /* 创建时间 */
            agentProfitSharingMcInfoBean.setCreateTime(tmPlatBizTmp.getCreateTime());
            /* 最后修改人 */
            agentProfitSharingMcInfoBean.setLastOprId(tmPlatBizTmp.getLastOprId());
            /* 修改时间 */
            agentProfitSharingMcInfoBean.setUpdateTime(tmPlatBizTmp.getLastUpdateTime());
            /* 审批拒绝原因 */
            agentProfitSharingMcInfoBean.setAuditRefuseReason(tmPlatBizTmp.getAuditRefuseReason());
            /* 发布拒绝原因 */
//            agentProfitSharingMcInfoBean.setPublishRefuseReason(tmPlatBizTmp.getPublishRefuseReason());
            /* 备用字段1 */
            agentProfitSharingMcInfoBean.setRemark1(tmPlatBizTmp.getRemark1());
            /* 备用字段2 */
            agentProfitSharingMcInfoBean.setRemark2(tmPlatBizTmp.getRemark2());
            /* 备用字段3 */
            agentProfitSharingMcInfoBean.setRemark2(tmPlatBizTmp.getRemark3());
            /* 备用字段4 */
            agentProfitSharingMcInfoBean.setRemark2(tmPlatBizTmp.getRemark4());
            /* 备用字段5 */
            agentProfitSharingMcInfoBean.setRemark2(tmPlatBizTmp.getRemark5());

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return agentProfitSharingMcInfoBean;
    }


    /**
     * 营销活动机构信息数据库实体类和接口实体类数据转换
     *
     * @param mcOrgInfo 营销活动机构信息表
     * @return cc.rengu.igas.momp.facade.bean.McOrgInfoBean 营销活动机构信息对象
     * @author Jinan Liu
     * @date 2020/4/1 10:33
     */
    private McOrgInfoBean convertMcOrgInfo(TMMcOrgInfo mcOrgInfo) {
        McOrgInfoBean mcOrgInfoBean = new McOrgInfoBean();
        try {
            /* 机构编号 */
            mcOrgInfoBean.setOrgId(mcOrgInfo.getOrgId());
            /* 机构层级 */
            mcOrgInfoBean.setOrgHierarchy(mcOrgInfo.getOrgHierarchy());
            /* 机构名称 */
            mcOrgInfoBean.setOrgName(mcOrgInfo.getOrgName());

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动机构信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcOrgInfoBean;
    }

    /**
     * 营销活动机构信息数据库临时表实体类和接口实体类数据转换
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.McOrgInfoBean 营销活动机构信息对象
     * @author Jinan Liu
     * @date 2020/5/28 20:01
     */
    private McOrgInfoBean convertTmpMcOrgInfo(TMPlatBizTmp tmPlatBizTmp) {

        McOrgInfoBean mcOrgInfoBean = new McOrgInfoBean();
        try {

            JsonTmpMcOrgInfo jsonTmpMcOrgInfo = JsonOperation.parseTmpMcOrgInfoBizData1(tmPlatBizTmp, rglog);

            /* 机构编号 */
            mcOrgInfoBean.setOrgId(jsonTmpMcOrgInfo.getOrgId());
            /* 机构层级 */
            mcOrgInfoBean.setOrgHierarchy(jsonTmpMcOrgInfo.getOrgHierarchy());
            /* 机构名称 */
            mcOrgInfoBean.setOrgName(jsonTmpMcOrgInfo.getOrgName());
            /* 操作类型 */
            mcOrgInfoBean.setOprType(jsonTmpMcOrgInfo.getOprType());

        } catch (Exception e) {
            rglog.error("转换临时表营销活动机构信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcOrgInfoBean;
    }

    /**
     * @param agentProfitSharingRuleInfo
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/25 23:15
     * @description 营销活动规则信息数据库实体类和接口实体类数据转换
     * @return: cc.rengu.igas.momp.facade.bean.AgentProfitSharingRuleInfoBean
     */
    private AgentProfitSharingRuleInfoBean convertMcRuleInfo(TMMcAgentProfitSharingRuleInfo agentProfitSharingRuleInfo) {

        AgentProfitSharingRuleInfoBean agentProfitSharingRuleInfoBean = new AgentProfitSharingRuleInfoBean();

        try {

            /* 顺序号  */
            agentProfitSharingRuleInfoBean.setRuleIndex(agentProfitSharingRuleInfo.getRuleIndex());
            /* 控制下限 */
            agentProfitSharingRuleInfoBean.setCriteriaFloor(agentProfitSharingRuleInfo.getCriteriaFloor());
            /* 控制上限 */
            agentProfitSharingRuleInfoBean.setCriteriaCelling(agentProfitSharingRuleInfo.getCriteriaCelling());
            /* 优惠类型 */
            agentProfitSharingRuleInfoBean.setDiscountType(agentProfitSharingRuleInfo.getDiscountType());
            /* 优惠力度 */
            agentProfitSharingRuleInfoBean.setDiscount(agentProfitSharingRuleInfo.getDiscount());

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动规则信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return agentProfitSharingRuleInfoBean;
    }

    /**
     * 营销活动时间信息数据库临时表实体类和接口实体类数据转换
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.MoneyOffRuleInfoBean 代理商分润类营销活动规则信息对象
     * @author Jinan Liu
     * @date 2020/5/28 20:30
     */
    private AgentProfitSharingRuleInfoBean convertTmpMcRuleInfo(TMPlatBizTmp tmPlatBizTmp) {

        AgentProfitSharingRuleInfoBean agentProfitSharingRuleInfoBean = new AgentProfitSharingRuleInfoBean();

        try {

            JsonTmApsMcRuleInfo jsonTmApsMcRuleInfo = JsonOperation.parseTmpApsMcRuleInfoBizData1(tmPlatBizTmp, rglog);

            /* 顺序号  */
            agentProfitSharingRuleInfoBean.setRuleIndex(jsonTmApsMcRuleInfo.getRuleIndex());
            /* 控制下限 */
            agentProfitSharingRuleInfoBean.setCriteriaFloor(jsonTmApsMcRuleInfo.getCriteriaFloor());
            /* 控制上限 */
            agentProfitSharingRuleInfoBean.setCriteriaCelling(jsonTmApsMcRuleInfo.getCriteriaCelling());
            /* 优惠类型 */
            agentProfitSharingRuleInfoBean.setDiscountType(jsonTmApsMcRuleInfo.getDiscountType());
            /* 优惠力度 */
            agentProfitSharingRuleInfoBean.setDiscount(jsonTmApsMcRuleInfo.getDiscount());
            /* 操作类型 */
            agentProfitSharingRuleInfoBean.setOprType(jsonTmApsMcRuleInfo.getOprType());

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动规则信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return agentProfitSharingRuleInfoBean;
    }

    /**
     * 使用商户编号从内管商户基本信息表查询商户所属商圈编号
     *
     * @param globalInstId 法人机构号
     * @param mchntNo      商户编号
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 13:06
     */
    private TBMchntBaseInfo selectMompMchntBaseInfoByPrimaryKey(String globalInstId, String mchntNo) throws Exception {

        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        return mchntInfoMapper.selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
    }

    /**
     * 银行录入员查询营销活动基本信息表数据(正式表)
     *
     * @param moMcListInfoInObj 营销活动列表查询使用实体类入参
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:11
     */
    private TMMcAgentProfitSharingInfo selectApsInfoDetailInfoByBankBasicRole(MoMcListInfoInObj moMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectApsInfoDetailInfoByBankBasicRole(moMcListInfoInObj);
    }

    /**
     * 银行发布员查询营销活动基本信息表数据
     *
     * @param moMcListInfoInObj 营销活动列表查询使用实体类入参
     * @return cc.rengu.igas.momp.common.entity.TMMcMoneyOffInfo 减免类营销活动信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 19:11
     */
    private TMMcAgentProfitSharingInfo selectApsInfoDetailInfoByBankPublishRole(MoMcListInfoInObj moMcListInfoInObj) throws Exception {
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        return mcDetailInfoMapper.selectApsInfoDetailInfoByBankPublishRole(moMcListInfoInObj);
    }

    /**
     * @param queryAgentProfitSharingMcDetailInfoRequest
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/25 22:37
     * @description 获取营销活动基本信息
     * @return: void
     */
    private TMMcAgentProfitSharingInfo getMcInfo(QueryAgentProfitSharingMcDetailInfoRequest queryAgentProfitSharingMcDetailInfoRequest) {

        String instId = queryAgentProfitSharingMcDetailInfoRequest.getHeader().getInstId();
        String mcNo = queryAgentProfitSharingMcDetailInfoRequest.getMcNo();

        try {
            McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
            TMMcAgentProfitSharingInfo mcAgentProfitSharingInfo = mcDetailInfoMapper.selectAgentProfitSharingMcInfo(instId, mcNo);
            if (mcAgentProfitSharingInfo != null) {
                return mcAgentProfitSharingInfo;
            } else {
                rglog.error("机构<{}>的代理商分润类营销活动<{}>信息查询失败!", instId, mcNo);
                throw new BizException(RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespDesc());
            }
        } catch (Exception e) {
            rglog.error("机构<{}>的代理商分润类营销活动<{}>信息查询失败!", instId, mcNo);
            throw new BizException(RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_AGENT_PROFIT_SHARING_MC_DETAIL_ERROR.getRespDesc());
        }
    }

    /**
     * 获取营销活动机构信息列表(正式表)
     *
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcOrgInfo> 营销活动机构信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 18:36
     */
    private List<TMMcOrgInfo> getAllMcOrgListInfo() throws Exception {

        McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
        return mcOrgInfoMapper.selectMcOrgInfoList(globalInstId, globalMcNo);
    }

}
